import 'dart:math';

import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter/widgets.dart';
import 'package:ls_work/net/api/http_api.dart';
import 'package:ls_work/ui/new_actvity/activity_sign_in_list_page.dart';
import 'package:ls_work/ui/new_actvity/volunteer_sign_in_page.dart';
import 'package:ls_work/util/ext_utils.dart';
import 'package:ls_work/widgets/load_image.dart';

import '../../net/response/activity/actvity_recommend_list_data.dart';
import '../../res/colors.dart';
import '../../viewmodel/new_activity/activity_info_detal_view_model.dart';
import '../../widgets/activity_widget.dart';
import 'old_sign_in_page.dart';

class ActivityInfoDetailPage extends BaseStatefulMvvmWidget {
  final ActvityRecommendListData data;

  ActivityInfoDetailPage(this.data);

  @override
  State<StatefulWidget> createState() => _ActivityInfoDetailState();
}

class _ActivityInfoDetailState
    extends BaseMvvmState<ActivityInfoDetailViewModel, ActivityInfoDetailPage> {
  @override
  Widget buildLoadingContentView() {
    return Column(
      children: [
        NewContent().setWeight(1),
        if (viewModel.canEnroll)
          Row(
            children: [
              TextButton(
                onPressed: () {
                  Go().push(VolunteerSignInPage(widget.data));
                },
                child: CommonText('志愿者报名', textColor: Colors.white),
                style: ButtonStyle(
                    backgroundColor: WidgetStateProperty.all(Colors.blue),
                    shape: WidgetStateProperty.all(StadiumBorder())),
              ).setWeight(1),
              Container(
                width: 16,
              ),
              TextButton(
                onPressed: () {
                  Go().push(OldSignInPage(widget.data));
                },
                child: CommonText('活动报名', textColor: Colors.white),
                style: ButtonStyle(
                    backgroundColor: WidgetStateProperty.all(Colors.green),
                    shape: WidgetStateProperty.all(StadiumBorder())),
              ).setWeight(1)
            ],
          ).addToContainer(
              padding: EdgeInsets.symmetric(horizontal: 16, vertical: 10))
      ],
    );
  }

  Widget OldContent() {
    return ListView(
      children: [
        AspectRatio(
          aspectRatio: 2,
          child: ImageLoad(
            "${HttpApi.imageHead}${widget.data.attachId}",
            fit: BoxFit.cover,
            width: MediaQuery.of(context).size.width,
          ),
        ),
        ...ActivityWidget.activityIndfoWidget(viewModel.contentList),
      ],
    );
  }

  Widget NewContent() {
    var info = viewModel.detailInfo;
    return info == null
        ? Container()
        : SingleChildScrollView(
            child: Stack(
              clipBehavior: Clip.none,
              children: [
                AspectRatio(
                  aspectRatio: 2,
                  child: ImageLoad(
                    "${HttpApi.imageHead}${widget.data.attachId}",
                    fit: BoxFit.cover,
                    width: MediaQuery.of(context).size.width,
                  ),
                ),
                Column(
                  children: [
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        CommonText(info.activityName, textSize: 18),
                        SizedBox(
                          height: 15,
                        ),
                        Wrap(
                          spacing: 15,
                          children: [
                            CommonText(info.activityTypeName,
                                textSize: 14, textColor: Colors.white)
                                .addToContainer(
                                decoration: BoxDecoration(
                                    color: Colors.red,
                                    borderRadius: 15.borderRadius),
                                padding: EdgeInsets.symmetric(
                                    horizontal: 13, vertical: 3)),
                            CommonText(info.activityLevelName,
                                textSize: 14, textColor: Colors.white)
                                .addToContainer(
                                decoration: BoxDecoration(
                                    color: Colors.green,
                                    borderRadius: 15.borderRadius),
                                padding: EdgeInsets.symmetric(
                                    horizontal: 13, vertical: 3))
                          ],
                        ),
                        SizedBox(
                          height: 15,
                        ),
                        Divider(
                          height: 1,
                        ),
                        SizedBox(
                          height: 15,
                        ),
                        ...[
                          Row(
                            children: [
                              Icon(
                                Icons.location_on,
                                size: 18,
                                color: Colors.grey,
                              ),
                              SizedBox(
                                width: 8,
                              ),
                              Text(info.placeName ?? "")
                            ],
                          ),
                          Row(
                            children: [
                              Icon(
                                Icons.person,
                                size: 18,
                                color: Colors.grey,
                              ),
                              SizedBox(
                                width: 8,
                              ),
                              Text(
                                  "报名人数(${viewModel.oldList.length}${info.limitHumans == 0 ? "" : "/${info.limitHumans}"})")
                            ],
                          ),
                          Row(
                            children: [
                              Icon(
                                Icons.access_time_sharp,
                                size: 18,
                                color: Colors.grey,
                              ),
                              SizedBox(
                                width: 8,
                              ),
                              Text(
                                  "${info.activityStartTime?.changeTime(format: [
                                    'mm',
                                    '/',
                                    'dd',
                                    ' ',
                                    HH,
                                    ':',
                                    nn
                                  ])}-${info.activityEndTime?.changeTime(format: [
                                    'mm',
                                    '/',
                                    'dd',
                                    ' ',
                                    HH,
                                    ':',
                                    nn
                                  ])}")
                            ],
                          ),
                          if (info.registrationStartTime != null &&
                              info.registrationStartTime!.isBeforeNow())
                            Row(
                              children: [
                                Icon(
                                  Icons.access_time_sharp,
                                  size: 18,
                                  color: Colors.grey,
                                ),
                                SizedBox(
                                  width: 8,
                                ),
                                Text(
                                    "${info.registrationStartTime?.changeTime(format: [
                                      'mm',
                                      '/',
                                      'dd',
                                      ' ',
                                      HH,
                                      ':',
                                      nn
                                    ])} 开始报名")
                              ],
                            ),
                          if (info.registrationStartTime != null &&
                              !info.registrationStartTime!.isBeforeNow())
                            Row(
                              children: [
                                Icon(
                                  Icons.access_time_sharp,
                                  size: 18,
                                  color: Colors.grey,
                                ),
                                SizedBox(
                                  width: 8,
                                ),
                                Text(
                                    "${info.registrationEndTime?.changeTime(format: [
                                      'mm',
                                      '/',
                                      'dd',
                                      ' ',
                                      HH,
                                      ':',
                                      nn
                                    ])} 报名截止")
                              ],
                            ),

                          Row(
                            children: [
                              Icon(
                                Icons.score,
                                size: 18,
                                color: Colors.grey,
                              ),
                              SizedBox(
                                width: 8,
                              ),
                              Text(
                                  "签到积分：${info.checkPoints}  志愿者积分：${info.volunteerPoints}")
                            ],
                          ),

                          Row(
                            children: [
                              Icon(
                                Icons.manage_accounts_rounded,
                                size: 18,
                                color: Colors.grey,
                              ),
                              SizedBox(
                                width: 8,
                              ),
                              Text(
                                  "负责人：${info.directorName}")
                            ],
                          ),
                        ].insertWidget(SizedBox(
                          height: 12,
                        ))
                      ],
                    ).addToContainer(
                        width: double.infinity,
                        margin: EdgeInsets.symmetric(horizontal: 16),
                        padding:
                        EdgeInsets.symmetric(horizontal: 15, vertical: 15),
                        decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: 10.borderRadius)),
                    if (info.isRegistration == 1) ...[
                      SizedBox(
                        height: 15,
                      ),
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            "已报名(${viewModel.oldList.length}${info.limitHumans == 0 ? "" : "/${info.limitHumans}"})",
                            style: TextStyle(color: Colors.black, fontSize: 16),
                          ),
                          SizedBox(
                            height: 12,
                          ),
                          LinearProgressIndicator(
                            value: info.isRegistration!=1?1:info.limitHumans==0?viewModel.oldList.isEmpty?0:1:(viewModel.oldList.length/info.limitHumans!),
                            minHeight: 16,
                            borderRadius: 15.borderRadius,
                            backgroundColor: Colors.grey.withOpacity(0.4),
                            color: Colours.app_main,
                          ),
                          SizedBox(height: 12,),
                          Stack(
                            children: List.generate(min(viewModel.oldList.length, 10), (index){
                              return LoadAssetImage('none',width: 30,height: 30,).toCircle(borderColor: Colors.grey,borderWidth: 1).setLocation(left: index*24);
                            }),
                          ).addToContainer(height: 32)
                        ],
                      ).addToContainer(
                          width: double.infinity,
                          margin: EdgeInsets.symmetric(horizontal: 16),
                          padding: EdgeInsets.symmetric(
                              horizontal: 15, vertical: 15),
                          decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: 10.borderRadius)),
                    ],

                    SizedBox(height: 15,),
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          "活动内容",
                          style: TextStyle(color: Colors.black, fontSize: 16),
                        ),
                        SizedBox(
                          height: 12,
                        ),
                        CommonText(info.activityContent)
                      ],
                    ).addToContainer(
                        width: double.infinity,
                        margin: EdgeInsets.symmetric(horizontal: 16),
                        padding: EdgeInsets.symmetric(
                            horizontal: 15, vertical: 15),
                        decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: 10.borderRadius)),

                    if(!info.remark.isNullOrEmpty())
                      ...[
                        SizedBox(height: 15,),
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                              "备注信息",
                              style: TextStyle(color: Colors.black, fontSize: 16),
                            ),
                            SizedBox(
                              height: 12,
                            ),
                            CommonText(info.remark)
                          ],
                        ).addToContainer(
                            width: double.infinity,
                            margin: EdgeInsets.symmetric(horizontal: 16),
                            padding: EdgeInsets.symmetric(
                                horizontal: 15, vertical: 15),
                            decoration: BoxDecoration(
                                color: Colors.white,
                                borderRadius: 10.borderRadius)),
                      ],

                    SizedBox(height: 20,)
                  ],
                ).addToContainer(margin: EdgeInsets.only(top: MediaQuery.of(context).size.width / 2 - 25))
                // Positioned(child: ,top: MediaQuery.of(context).size.width / 2 - 25,left: 0,right: 0,)
              ],
            ),
          );
  }



  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      backgroundColor: Colours.bg_color,
      appBar: AppBar(
        title: Text('活动名称'),
        actions: [
          TextButton(
              onPressed: () {
                showBottomSelect(context, items: ["志愿者签到", "活动签到"],
                    onSelect: (value) {
                  if (value == "志愿者签到") {
                    Go().push(
                        ActivitySignInListPage(1, widget.data.id.toString()));
                  } else {
                    Go().push(
                        ActivitySignInListPage(0, widget.data.id.toString()));
                  }
                });
              },
              child: CommonText("活动签到", textColor: Colours.app_main))
        ],
      ),
      body: loadingContentWidget,
    );
  }

  @override
  ActivityInfoDetailViewModel createViewModel() {
    return ActivityInfoDetailViewModel(context, widget.data);
  }

  @override
  void onRetryClick() {}
}
